<?php

namespace app\index\controller;

use app\common\model\Member as MemberModel;
use app\common\model\Manager as ManagerModel;
use app\common\model\ManagerJob as ManagerJobModel;
use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\ManagerCompany as ManagerCompanyModel;
use app\common\model\ManagerPassword as ManagerPasswordModel;

class Manager extends Base
{
    /**
     * [getIndex  僧侣列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $mcmodel = new ManagerCompanyModel;
        $mpmodel = new ManagerPasswordModel;
        $mmodel = new ManagerDutyModel;
        $mjmodel = new ManagerJobModel;
        try {
            if(!empty($param['con'])){
                $map['ma.tel|ma.name|ma.username'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['sex'])){
                $map['me.sex'] = $param['sex'];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['ma.addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['mc.comid'] = $user['comid'];
            $map['mc.status'] = 1;
            $count = $mcmodel->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $mcmodel->getList($map, $this->from, $this->size);

            foreach($lists as &$v){
                $mpass = $mpmodel->getOne(['comid'=>$user['comid'],'mid'=>$v['id']],'id','states');
                $v['states'] = $mpass['states'];
                if($v['dutyid'] == 0){
//                    $mpmodel->editData(['id'=>$v['mcid'],'dutyid'=>2]);
                    $v['duty'] = '管理组(默认)';
//                    $v['duty'] = '住持';
                }else{
                    $duty = $mmodel->findData($v['dutyid'],'id','duty');
                    $v['duty'] = $duty['duty'];
                }
                if(empty($v['jobid'])){
                    $v['job'] = '暂无';
                }else{
                    $duty = $mjmodel->findData($v['jobid'],'id','job');
                    $v['job'] = $duty['job'];
                }
                if(!empty($v['conversiontime'])){
                    $v['addtime'] = date('Y-m-d H:i:s',$v['conversiontime']);
                }else{
                    $v['addtime'] = '暂无';
                }


                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('index.domain_name').$v['img'];
                }else{
                    $v['img'] = $v['img'];
                }

                if($v['sex'] == 1){
                    $v['sex'] = '男';
                }elseif($v['sex'] == 2){
                    $v['sex'] = '女';
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }

        writelog($user['mid'],$user['comid'],'查看法师列表');

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postAdd  僧侣添加]
     * @param type 1 查询用户法师是否存在
     * @param ftype 2 法师不存在 3 法师已存在 4 用户不存在
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $model = new ManagerModel;
        $memodel = new MemberModel;
        $mcmodel = new ManagerCompanyModel;
        $mpmodel = new ManagerPasswordModel;
        if(empty($param['img'])){
            $param['img'] = 'https://fushan.oss-cn-hangzhou.aliyuncs.com/nouser.png';
        }
        $manager = $model->findData($param['tel'],'tel','id,tel,name,username');
        $member = $memodel->findData($param['tel'],'tel','id,sex,bathday,img');
        if(!empty($param['type'])){
            if($param['type'] == 1){
                if(!empty($member)){
                    $http = substr($member['img'],0,4);
                    if($http != 'http'){
                        $member['img'] = config('index.domain_name').$member['img'];
                    }
                    if(empty($manager)){
                        return admin_json(2,'法师不存在',$member);
                    }
                    $data = array(
                        'img' => $member['img'],
                        'name' => $manager['name'],
                        'username' => $manager['username'],
                        'tel' => $manager['tel'],
                        'sex' => $member['sex'],
                        'bathday' => $member['bathday'],
                    );
                    return admin_json(3,'法师已存在',$data);
                }else{
                    return admin_json(4,'用户不存在');
                }
            }
        }
        try{
            $pswd = $param['pswd'];
            if($param['ftype'] == 4){
                $param['addtime'] = time();
                $param['identity'] = 2;
                $param['pswd'] = md5( $param['pswd']);
                $member = $memodel->findData($param['tel'],'tel','id,sex,bathday,img');
                if(empty($member)){
                    $param['register_comid'] = $user['comid'];
                    $memodel->addData($param);
                }
            }else{
                $data = array();
                $data = array(
                    'identity' => 2,
                    'id' => $member['id'],
                );
                $memodel->editData($data);
                unset($param['id']);
            }

            if($param['ftype'] != 3){
                $manager = $model->findData($param['tel'],'tel','id,tel,name,username');
                if(empty($manager)){
                    $id = $model->addData($param);
                }
            }else{
                $id = $manager['id'];
                $model->editData(['id'=>$id,'name'=>$param['name']]);
            }

            $mc = $mcmodel->getOne(['comid'=>$user['comid'],'mid'=>$id]);
            if(empty($mc)){
                $data = array();
                $data = array(
                    'comid' => $user['comid'],
                    'dutyid' => $param['dutyid'],
                    'jobid' => $param['jobid'],
                    'mid' => $id,
                    'addtime' => time()
                );
                $mcmodel->addData($data);
                $data = array();
                $data = array(
                    'comid' => $user['comid'],
                    'mid' => $id,
                    'password' => md5(md5($pswd) . config('index.manager_password_halt')),
                    'addtime' => time(),
                    'pass' => $pswd,
                    'tel' => $param['tel'],
                    'dutyid' => $param['dutyid'],
                    'jobid' => $param['jobid'],
                );
                if($param['dutyid'] == 1){
                    $data['states'] = -1;
                }
                $mpmodel->addData($data);
            }else{
                if($mc['status'] == 1){
                    return admin_json(-1,'法师已存在！');
                }
                $data = array();
                $data = array(
                    'id'=>$mc['id'],
                    'dutyid' => $param['dutyid'],
                    'jobid' => $param['jobid'],
                    'status'=>1
                );
                $mcmodel->editData($data);

                $mpass= $mpmodel->getOne(['comid'=>$user['comid'],'mid'=>$id]);
                if(!empty($mpass)){
                    $data = array();
                    $data = array(
                        'id'=>$mpass['id'],
                        'dutyid' => $param['dutyid'],
                        'jobid' => $param['jobid'],
                    );
                    if($param['dutyid'] == 1){
                        $data['states'] = -1;
                    }else{
                        $data['states'] = 1;
                    }
                    $mpmodel->editData($data);
                }else{
                    $data = array();
                    $data = array(
                        'comid' => $user['comid'],
                        'mid' => $id,
                        'password' => md5(md5($pswd) . config('index.manager_password_halt')),
                        'addtime' => time(),
                        'pass' => $pswd,
                        'tel' => $param['tel'],
                        'dutyid' => $param['dutyid'],
                        'jobid' => $param['jobid'],
                    );
                    if($param['dutyid'] == 1){
                        $data['states'] = -1;
                    }
                    $mpmodel->addData($data);
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,'添加失败'.$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'添加法师'.$id.'成功');
        return admin_json(1,'添加成功');
    }
    /**
     * [postEdit  僧侣修改]
     * @param ftype 2 法师不存在 3 法师已存在 4 用户不存在
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $user = $this->user;
        $memodel = new MemberModel;
        $model = new ManagerModel;
        $mcmodel = new ManagerCompanyModel;
        $mpmodel = new ManagerPasswordModel;
        $map = [];
        try{
            $manager = $model->findData($param['tel'],'tel','id');
            $map['tel'] = $param['tel'];
            $memodel->editDataBy($param,$map);
            $mcmodel->editDataBy($param,['mid'=>$manager['id'],'comid'=>$user['comid']]);
            $mpmodel->editDataBy($param,['mid'=>$manager['id'],'comid'=>$user['comid']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,'修改失败'.$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'修改法师'.$param['tel'].'成功');
        return admin_json(1,'修改成功');
    }
}